<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
    <title>登录</title>
    <link rel="stylesheet" href="css/login.css" />
    <style>

    </style>
</head>

<body>
    <div id="login-bg">
        <div id="login">
            <div id="login-title">登录</div>
            <div id="login-label">请使用您的邮箱登录</div>
            <div class="login-form-input">
                <img class="login-form-input-icon" src="images/username.png" />
                <input type="text" id="username" placeholder="邮箱">
            </div>
            <div class="login-form-input">
                <img class="login-form-input-icon" src="images/password.png" />
                <input type="password" id="password" placeholder="密码">
            </div>
            <button class="login-form-btn" onclick="login()">登录</button>
            <a class="login-to-register" onclick="register()">注册新账号</a>
        </div>
    </div>
</body>

<!-- Various Scripts -->
<script src="./js/jquery-3.2.1.min.js"></script>

<script>
    function login() {
        var email = $('#username').val();
        var password = $('#password').val();
        if(email == ""){
            alert("请输入邮箱再进行登录!");
            return;
        }
        var param = {};
        param.eamil = email;
        param.password = password;
        var url = "http://localhost/api/user/login/"+email+"/"+password;
        $.ajax({
                //请求方式
                type : "POST",
                //请求的媒体类型
                contentType: "application/json;charset=UTF-8",
	            url: url,
                xhrFields: {
                    withCredentials: true
                },
                data:"",
                success: function(data) {
                    if (data.code == '0000') {
                        alert("登录成功！");
                        var cookie = document.cookie;
                        console.log(cookie);
                        window.location.href = "welcome.html";
                    } else {
                        alert("登录失败，原因为："+data.msg);
                    }
                },
                //请求失败，包含具体的错误信息
                error: function(e){
                    alert("登录失败");
                }
        });
    }

    function register(){
        window.location.href = "register.html";
    }
</script>

</html>